package com.ruoyi.prodata;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.project.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author 24822
 */
@RestController
@RequestMapping("/prodata/projectStatistics")
public class ProjectStatisticsController extends BaseController {

    @Autowired
    private IProjectService projectService;

    @GetMapping("/list")
    public AjaxResult list(){
        List<Map<String,Object>> list = projectService.list();
        List<String> projectNameList = new ArrayList<>();
        List<String> paidList = new ArrayList<>();
        List<String> targetList = new ArrayList<>();
//        list.forEach(map->{
//            projectNameList.add(map.get("name").toString());
//            paidList.add(map.get("paid").toString());
//            targetList.add(map.get("target").toString());
//        });
        //优化: 使用Optional, 避免空指针
        list.forEach(map -> {
            projectNameList.add(Optional.ofNullable(map.get("name")).map(Object::toString).orElse("N/A"));
            paidList.add(Optional.ofNullable(map.get("paid")).map(Object::toString).orElse("0"));
            targetList.add(Optional.ofNullable(map.get("target")).map(Object::toString).orElse("0"));
        });
        Map<String,Object> result = new HashMap<>();
        result.put("projectNameList",projectNameList);
        result.put("paidList",paidList);
        result.put("targetList",targetList);
        return AjaxResult.success(result);
    }

}
